package com.lkj;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.util.CharsetUtil;

import java.util.UUID;
import java.util.concurrent.TimeUnit;

public class SomeSocketServerHandler extends ChannelInboundHandlerAdapter {

    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        System.out.println(ctx.channel().remoteAddress()+", "+msg);

//        ctx.channel().writeAndFlush("from server:"+ UUID.randomUUID());
//        TimeUnit.MILLISECONDS.sleep(500);
        String msgStr = (String)msg;
        String[] split = msgStr.split("\r\n");
        for (String s : split) {
            ByteBuf content = Unpooled.copiedBuffer(s + System.getProperty("line.separator"), CharsetUtil.UTF_8);

            ctx.channel().writeAndFlush(content);
        }

    }


    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        cause.printStackTrace();
        ctx.close();
    }
}
